'use strict';
import { StackActions, NavigationActions } from 'react-navigation'

class RouterHelper {

    // navigation的实例
    static get navigation() {
        return this.routeStack.length !== 0 ? this.routeStack[this.routeStack.length - 1] : null
    };
    // 上次执行to方法的时间
    static lastActionTime = 0;
    // 重复点击判断间隔时间,单位毫秒
    static interval = 600;
    // 列表保存路由栈信息
    static routeStack = [];
    // 未登录忽略名单，跳转时跳到登录界面
    static noLoginIgnoreRoute = []

    static checkActionState() {
        if (!this.navigation) {
            console.error('请先初始化路由');
            return false
        }
        const nowTime = new Date().getTime();
        if ((nowTime - this.lastActionTime) <= this.interval) {
            console.warn('间隔时间内重复点击了');
            return false
        }
        this.lastActionTime = nowTime;
        return true
    }

    static addStack(navigation) {
        const index = this.routeStack.findIndex((item) => navigation.state.key === item.state.key);
        if (index === -1) {
            this.routeStack.push(navigation);
        }
    }

    static remove(navigation) {
        const index = this.routeStack.findIndex((item) => navigation.state.key === item.state.key);
        if (index !== -1) {
            this.routeStack.splice(index, 1);
        }
    }

    // 最好使用这个
    static navigate(pageTitle, routeName, params = {}) {
        params = {
            pageTitle,
            ...params
        };
        if (!this.checkActionState()) {
            return;
        }
        // 未登录判断的规则
        if (global.loginState === 0 && this.noLoginIgnoreRoute.length > 0) {
            const index = this.noLoginIgnoreRoute.findIndex((item) => item === routeName);
            if (index === -1) {
                routeName = 'LoginAndRegistered'
            }
        }
        this.navigation.navigate(routeName, params);
    }

    static push(pageTitle, routeName, params = {}) {
        params = {
            pageTitle,
            ...params
        };
        if (!this.checkActionState()) {
            return;
        }
        // 未登录判断的规则
        if (global.loginState === 0 && this.noLoginIgnoreRoute.length > 0) {
            const index = this.noLoginIgnoreRoute.findIndex((item) => item === routeName);
            if (index === -1) {
                routeName = 'LoginAndRegistered'
            }
        }
        this.navigation.push(routeName, params);
    }

    static goBack(routeName) {
        if (!this.checkActionState()) {
            return;
        }
        if (routeName) {
            const index = this.routeStack.findIndex((item) => routeName === item.state.routeName)
            const navTarget = this.routeStack[index + 1]
            const key = navTarget.state.key;
            this.navigation.goBack(key);
        } else {
            this.navigation.goBack()
        }
    }

    static pop(n, params = {}) {
        if (!this.checkActionState()) {
            return;
        }
        this.navigation.pop(n, params)
    }

    static popToTop(params = {}) {
        if (!this.checkActionState()) {
            return;
        }
        this.navigation.popToTop(params)
    }

    static replace(pageTitle, routeName, params = {}) {
        params = {
            pageTitle,
            ...params
        };
        if (!this.checkActionState()) {
            return;
        }
        this.navigation.replace(routeName, params)
    }

    static reset(pageTitle, routeName, params = {}) {
        params = {
            pageTitle,
            ...params
        };
        if (!this.checkActionState()) {
            return;
        }
        let resetAction = StackActions.reset({
            index: 0,
            actions: [
                NavigationActions.navigate({ routeName, params })
            ]
        });
        this.navigation.dispatch(resetAction);
    }
}

export default RouterHelper
